<div class='right_content_inner_<?=$module?>'>
	<script type="text/javascript">
		var is_apply = false;
		var is_new = false;
		var validator =false;
		AudioPlayer.setup("<?=JS_URL?>/audio-player/player.swf", {  
				width: 250,
				transparentpagebg: "yes"
			});  
			
		function save(){
			var options = {
				beforeSubmit:  showRequest,  // pre-submit callback
				success:       showResponse  // post-submit callback
			};
			
			<?php
			foreach($form_view_fields as $k => $form_view_config){
				if($form_view_config['type'] == 'textarea' && !empty($form_view_config['editor'])){
			?>
					$('#<?=$form_view_config['input_name']?>Editor').val(oEdit<?=$form_view_config['input_name']?>.getHTMLBody());
			<?php
				}
			}
			?>
			$('#frmManagement').ajaxSubmit(options);
		}

		function save_new(){
			is_new = true;
			save();
		}
		
		function apply(){
			is_apply = true;
			save();
		}

		function showRequest(formData, jqForm, options) {
			var flag = false;
			// validate editor
			<?php 
			foreach($form_view_fields as $k => $form_view_config){
				if($form_view_config['type'] == 'textarea' && !empty($form_view_config['editor'])){
			?>
					$('#<?=$form_view_config['input_name']?>Editor').css('visibility','hidden').css('position', 'absolute').show();
			<?php
				}
			}
			?>
		
			if(validator){
				if(validator.form()){
					flag = true;
				} else {
					validator.showErrors();
					// show_perm_denied(); // TODO Why???
				}
				$('#loader').fadeOut(300);
				return flag;
			}
		}

		function showResponse(responseText, statusText, xhr, $form) {
			var result;
			try {
				result = $.parseJSON(responseText);
			} catch(e){
				// console.log(e.message);
			}
			if(result && result.message == 'success'){
				if(is_apply){
					location.href=root+"admincp/"+module+"/update/"+result.obj_id;
				} else if(is_new){
					location.href=root+"admincp/"+module+"/update";
				} else {
					location.href=root+"admincp/"+module+"/#/save";
				}
			}
			
			if(responseText=='permission-denied'){
				show_perm_denied();
			}
			
			if(responseText=='File not support'){
				alert('File not support');
				$('#loader').fadeOut(300);
				show_perm_denied();
			}
			
		}

		// Config editor
		function config_editor(editor){
			editor.width = '732px';
			editor.height = '425px';
			editor.cmdAssetManager="modalDialogShow('"+root+"static/editor/assetmanager/assetmanager.php',640,445);";
			editor.css="<?=PATH_URL.'static/css/tv_app/detail.css?r=3'?>";
		}
		
		$(document).ready(function(){
			$('#frmManagement').submit(function(){
				save();
				return false;
			});
			
			$('.video-fancy').fancybox({
				onStart: function(e){
					player = e.attr('rel');
				},
				onClosed: function(e){
					if(player != ''){
						jwplayer(player).stop();
						player = '';
					}
				}
			});	
		
			<?php
			// setup form  validate rules
			$rules = array();
			foreach($form_view_fields as $k=>$form_view_config){
				if(isset($form_view_config['validate_type'])){
					$field_rules = explode('|', $form_view_config['validate_type']);
					$options = $form_view_config['input_name'] .': {';
					foreach($field_rules as $key=>$form_view_configal){
						$options .= $form_view_configal;
						if($key < count($field_rules) -1)
							$options .= ',';
					} 
					$options .='}';
					$rules[] = $options;
				}
			}
			
			if(!empty($rules)) {
			?>
			validator = $('#frmManagement').validate({
				rules : {
				<?php  foreach($rules as $k => $v) {
					echo $v;
					if($k < count($rules) -1)
						echo ",";
				} ?>
				}
			});
			<?php
			}
			?>
		})
		
		
		// HOT KEYS
		shortcut.add("Ctrl+Alt+B",
			function() {
				console.log('Ctrl+Alt+B');
			}
		);
		shortcut.add("Ctrl+Alt+A",
			function() {
				apply();
			}
		);
		shortcut.add("Ctrl+Alt+S",
			function() {
				save();
			}
		);
		shortcut.add("Ctrl+Alt+N",
			function() {
				save_new();
			}
		);
	</script>
	<div class="gr_perm_error" style="display:none;">
		<p><strong>FAILURE: </strong><span id="txt_error">Permission Denied.</span></p>
	</div>
	<div class="table">
		<div class="head_table"><div class="head_title_edit"><?=$module?></div></div>
		<div class="clearAll"></div>

		<form id="frmManagement" action="<?=PATH_URL.'admincp/'.$module.'/save/'?>" method="post" enctype="multipart/form-data">
			<input type="hidden" value="<?=$id?>" name="hiddenIdAdmincp" id="hiddenIdAdmincp" />
			
			<?php 
			foreach($form_view_fields as $k => $v){
				echo $v['html'];
			}
			?>
		</form>
	</div>
</div>